package cn.hl.hm.controller;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import cn.hl.hm.constants.SourceStatus;
import cn.hl.hm.dao.VtVlMapper;
import cn.hl.hm.entity.DriverRoute;
import cn.hl.hm.entity.Route;
import cn.hl.hm.entity.RouteVehiclePrice;
import cn.hl.hm.entity.STSPrice;
import cn.hl.hm.entity.VehicleType;
import cn.hl.hm.entity.VtVl;
import cn.hl.hm.service.DriverRouteService;
import cn.hl.hm.service.DriverService;
import cn.hl.hm.service.RouteService;
import cn.hl.hm.service.RouteVehiclePriceService;
import cn.hl.hm.service.StsPriceService;
import cn.hl.hm.util.BeanPicUtil;
import cn.hl.hm.util.InfoCode;
import cn.hl.hm.util.Result;
import cn.hl.hm.util.chinapay.Constant;

@Controller
@RequestMapping("/stsPriceController")
public class StsPriceController {
	private static final Logger logger = LoggerFactory.getLogger(DriverController.class);

	@Autowired
	StsPriceService stsPriceService;
	 @Autowired
	    private BeanPicUtil beanPicUtil;
	 @Autowired
		VtVlMapper vtVlMapper;
		@Autowired
		RouteService routeService;
		@Autowired
		RouteVehiclePriceService rvpService;
	/**
	 * 价格管理——查询列表
	 * @param driverId
	 * @param pageId
	 * @param pageCount
	 * @param request
	 * @param response
	 * @return
	 * @throws IOException
	 */
	@RequestMapping(value = "/selectInfo", method = RequestMethod.POST)
	public String selectDriverRouteList(STSPrice stsPrice, Integer pageId,
	        Integer pageCount,
			HttpServletRequest request, HttpServletResponse response) throws IOException {
		logger.info("园区收费---出车类型和车辆类型信息---stsPriceController/selectInfo");
		request.setCharacterEncoding("UTF-8");
		 
		try{
			List<STSPrice> STSPriceList = stsPriceService.selectStsPrice(stsPrice);
			if (null != STSPriceList&&STSPriceList.size()>0) { 
				List<STSPrice> sList = new ArrayList<>();
				for (STSPrice stsPrice2 : STSPriceList) {
					sList.add(beanPicUtil.modifyUrl(stsPrice2));
					
				}
				Result.ok(sList, InfoCode.SUCCESS, response);
				return null;
			} else {
				Result.ok(null, InfoCode.SUCCESS, response);
				return null;

			}
		}catch(Exception e){
			Result.ok(null, InfoCode.FAILURE, response);
			return null;
		}
		
	}
	
	
	/**
	 * 查询配载费 
	 * 
	 * @param driverId
	 * @param pageId
	 * @param pageCount
	 * @param request
	 * @param response
	 * @return
	 * @throws IOException
	 */
	@RequestMapping(value = "/selectStsPriceById", method = RequestMethod.POST)
	public String selectStsPriceById(STSPrice stsPrice,
			HttpServletRequest request, HttpServletResponse response) throws IOException {
		logger.info("订单---根据车型获取满载或空载价格---stsPriceController/selectStsPriceById");
		request.setCharacterEncoding("UTF-8");
		 
		try{
			if(stsPrice.getVtVlId()!=null){
				STSPrice ss = new STSPrice();
				ss.setVtVlId(stsPrice.getVtVlId());
				List<STSPrice> STSPriceList  = stsPriceService.selectStsPrice(ss);
				if(STSPriceList!=null && STSPriceList.size()>0){
					stsPrice.setStsVehicleType(STSPriceList.get(0).getStsVehicleType());
				}
			}
			List<STSPrice> STSPriceList = stsPriceService.selectStsPriceById(stsPrice);
			if (null != STSPriceList&&STSPriceList.size()>0) { 
				Result.ok(STSPriceList, InfoCode.SUCCESS, response);
				return null;
			} else {
				// 根据关系id 查询查询，然后查询该车型对应的所有关系id
				VtVl vv = new VtVl();
				vv.setId(stsPrice.getVtVlId());
				List<VtVl> l = vtVlMapper.selectVtVlList(vv);
				if(l!=null && l.size()>0){
					Integer vtId = l.get(0).getVehicleTypeId();
					VtVl vvv = new VtVl();
					// 根据车型ID获取所有该车型对应的关系ID
					vvv.setVehicleTypeId(vtId);
					List<VtVl> ll = vtVlMapper.selectVtVlList(vvv);
					for (VtVl vtVl : ll) {
						// 获取关系ID
						Integer vtvlId = vtVl.getId();
						STSPrice ss = new STSPrice();
						ss.setVtVlId(vtvlId);
						List<STSPrice> stsList  = stsPriceService.selectStsPrice(ss);
						if(stsList!=null && stsList.size()>0){
							STSPrice sts = new STSPrice();
							sts.setStsVehicleType(stsList.get(0).getStsVehicleType());
							
							List<STSPrice> stsList2 = stsPriceService.selectStsPriceById(sts);
							if (null != stsList2&&stsList2.size()>0) { 
								if(Constant.discountNum>0){
									    List<STSPrice> sss = new ArrayList<>();
										for (STSPrice stsPrice2 : stsList2) {
											stsPrice2.setStsPrice(stsPrice2.getStsPrice()*Constant.discountNum);
											sss.add(stsPrice2);
										}
										Result.ok(sss, InfoCode.SUCCESS, response);
									}
								else{
								Result.ok(stsList2, InfoCode.SUCCESS, response);
								}
								return null;
							}
						}
					}
				}
				
				
				
				Result.ok(new ArrayList(), InfoCode.SUCCESS, response);
				return null;

			}
		}catch(Exception e){
			e.printStackTrace();
			Result.ok(null, InfoCode.FAILURE, response);
			return null;
		}
		
	}
	/**
	 * chaxun  peizaifei --kongzai  manzai
	 * 
	 * @param driverId
	 * @param pageId
	 * @param pageCount
	 * @param request
	 * @param response
	 * @return
	 * @throws IOException
	 */
	@RequestMapping(value = "/selectVehicleList", method = RequestMethod.POST)
	public String selectVehicleList(
			HttpServletRequest request, HttpServletResponse response) throws IOException {
		logger.info("订单---获取车辆类型---stsPriceController/selectVehicleList");
		request.setCharacterEncoding("UTF-8");
		 
		try{
			List<STSPrice> STSPriceList = stsPriceService.selectVehicleList(null);
			if (null != STSPriceList&&STSPriceList.size()>0) { 
				Result.ok(STSPriceList, InfoCode.SUCCESS, response);
				return null;
			} else {
				Result.ok(null, InfoCode.SUCCESS, response);
				return null;

			}
		}catch(Exception e){
			Result.ok(null, InfoCode.FAILURE, response);
			return null;
		}
		
	}
	  /**
     * @Title: send  
     * @Description:添加价格
     * @date: 2017年1月5日
     * @author: 星矢
     * @param appId
     * @param order
     * @param request
     * @param response
     * @return String 
     * @throws
    */
	@RequestMapping(value = "/addStsPrice", method = RequestMethod.POST)
	public String addStsPrice(
			STSPrice stsPrice,
		HttpServletRequest request, HttpServletResponse response)  {
		logger.info("园区收费---添加价格---stsPriceController/addStsPrice");
		try{
//			VtVl vv = new VtVl();
//			vv.setVehicleTypeId(stsPrice.getVehicleTypeId());
//			vv.setVehicleLengthId(stsPrice.getVehicleLengthId());
//			List<VtVl> l  = vtVlMapper.selectVtVlList(vv);
//			if(l==null || l.size()==0){
////				 Result.ok(null, InfoCode.VEHICLE_ERROR, response);
////				 return null;
//				// 如果不存在，则执行插入，同时向路线-车辆-价格表中新增数据（所有路线均可 包含该车型）
//				VtVl v = new VtVl();
//				if (stsPrice.getVehicleTypeId() != null) {
//					v.setVehicleTypeId(stsPrice.getVehicleTypeId());
//				}
//				v.setVehicleLengthId(stsPrice.getVehicleLengthId());
//				v.setCreateTime(new Date().toLocaleString());
//				int i = vtVlMapper.insertSelective(v);
//				if (i > 0) {
//					stsPrice.setVtVlId(v.getId() );
//				}
//				// 获取车型
//				VehicleType vt = new VehicleType();
//				vt.setStatus("0");
//				Route r = new Route();
//				r.setRouteStatus("0");
//				List<Route> route = routeService.selectRoute(null);
//				// 增加一个车型，则所有路线都增加该车型double
//				for (Route route2 : route) {
//					RouteVehiclePrice rvp = new RouteVehiclePrice();
//					rvp.setPrice(new BigDecimal(0));
//					rvp.setRouteId(route2.getId());
//					rvp.setDateTime(new Date().toLocaleString());
//					rvp.setVtVlId(stsPrice.getVtVlId());
//					rvpService.addRouteVehiclePrice(rvp);
//				}
//			}else{
//				stsPrice.setVtVlId(l.get(0).getId());
//				STSPrice st = new STSPrice();
//				st.setChargeType(stsPrice.getChargeType());
//				st.setIsFullLoad(stsPrice.getIsFullLoad());
//				st.setStsVehicleType(stsPrice.getStsVehicleType());
//				st.setVtVlId(stsPrice.getVtVlId());
//				List ll = stsPriceService.selectStsPrice(st);
//				if(ll!=null && ll.size()>0){
//					 Result.ok(null, InfoCode.VEHICLE_ADD_ERROR, response);
//					 return null;
//				}
//			}
			STSPrice st = new STSPrice();
			st.setChargeType(stsPrice.getChargeType());
			st.setIsFullLoad(stsPrice.getIsFullLoad());
			st.setStsVehicleType(stsPrice.getStsVehicleType());
			st.setVehicleTypeId(stsPrice.getVehicleTypeId());
//			st.setVtVlId(stsPrice.getVtVlId());
			List ll = stsPriceService.selectStsPrice(st);
			if(ll!=null && ll.size()>0){
				 Result.ok(null, InfoCode.VEHICLE_ADD_ERROR, response);
				 return null;
			}
			stsPrice.setCreateTime(new Date().toLocaleString());
			stsPrice.setStsStatus(0);
			int add = stsPriceService.addStsPrice(stsPrice);
			if(add>0){
				Result.ok(null, InfoCode.SUCCESS, response);
				return null;
			}else {
				Result.ok(null, InfoCode.FAILURE, response);
				return null;
			}
		}catch(Exception e){
			e.printStackTrace();
			Result.ok(null, InfoCode.FAILURE, response);
			return null;
		}
		
	}
	 /**
     * @Title: send  
     * @Description:删除价格
     * @date: 2017年1月5日
     * @author: 星矢
     * @param appId
     * @param order
     * @param request
     * @param response
     * @return String 
     * @throws
    */
	@RequestMapping(value = "/deleteStsPrice", method = RequestMethod.POST)
	public String deleteStsPrice(
			STSPrice stsPrice,
		HttpServletRequest request, HttpServletResponse response)  {
		logger.info("园区收费---删除价格---stsPriceController/deleteStsPrice");
		try{
			 
			 
			stsPrice.setStsStatus(1); 
			int delete = stsPriceService.updateStsPrice(stsPrice);
			if(delete>0){
				Result.ok(null, InfoCode.SUCCESS, response);
				return null;
			}else {
				Result.ok(null, InfoCode.FAILURE, response);
				return null;
			}
		}catch(Exception e){
			e.printStackTrace();
			Result.ok(null, InfoCode.FAILURE, response);
			return null;
		}
		
	}
	 /**
     * @Title: send  
     * @Description:修改价格信息
     * @date: 2017年1月5日
     * @author: 星矢
     * @param appId
     * @param order
     * @param request
     * @param response
     * @return String 
     * @throws
    */
	@RequestMapping(value = "/updateStsPrice", method = RequestMethod.POST)
	public String updateStsPrice(
			STSPrice stsPrice,
//			Integer stsId,
		HttpServletRequest request, HttpServletResponse response)  {
		logger.info("园区收费---修改价格---stsPriceController/updateStsPrice");
//		STSPrice updatestsPrice = new STSPrice();
//		updatestsPrice.setId(stsId);
		try{
//			List<STSPrice> stsPriceList = stsPriceService.selectStsPrice(updatestsPrice);
//			if (null != stsPriceList&&stsPriceList.size()>0) {
//				STSPrice  deleteSTSPrice = stsPriceList.get(0);
//				deleteSTSPrice.setStsStatus(1); 
				if(stsPrice.getStsVehicleType()!=null && stsPrice.getIsFullLoad()!=null &&stsPrice.getStsPrice()!=null){
					
				}
				int update = stsPriceService.updateStsPrice(stsPrice); //  更新单条信息
				
				// 批量更新价格信息
				STSPrice ss = new STSPrice();
				ss.setStsVehicleType(stsPrice.getStsVehicleType());
				ss.setIsFullLoad(stsPrice.getIsFullLoad());
				ss.setStsPrice(stsPrice.getStsPrice());
				stsPriceService.updateStsPriceById(ss);
//				List<STSPrice> l = stsPriceService.selectStsPrice(ss);
//				for (STSPrice stsPrice2 : l) {
//					stsPrice2.setStsVehicleType(stsPrice.getStsVehicleType());
//					stsPrice2.setIsFullLoad(stsPrice.getIsFullLoad());
//					stsPrice2.setStsPrice(stsPrice.getStsPrice());
//					stsPriceService.updateStsPriceById(stsPrice2);
//				}
//				stsPrice.setStsStatus(0);
//				stsPrice.setCreateTime(new Date().toLocaleString());
//				int add = stsPriceService.addStsPrice(stsPrice);
				if(update>0){
				Result.ok(null, InfoCode.SUCCESS, response);
				return null;
			} else {
				Result.ok(null, InfoCode.FAILURE, response);
				return null;

			}
		}catch(Exception e){
			e.printStackTrace();
			Result.ok(null, InfoCode.FAILURE, response);
			return null;
		}
		
		
		 
		
	}
}
